package com.green.common.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Vector;

/**
 * 复制对象 工具类
 * @author ouyangyi
 *
 */
public class ObjectCopyUtils {

	public ObjectCopyUtils() {

	}

	/**
	 * 复制对象 把source的属性值 拷到dest对象
	 * @param source
	 * @param dest
	 */
	public static void copy(Object source, Object dest) {

		Method s[] = source.getClass().getMethods();

		Method d[] = dest.getClass().getMethods();

		try {

			for (int i = 0; i < d.length; i++) {

				String dname = d[i].getName();

				String dformat = dname.substring(3);
				if (dname.startsWith("set")) {

					for (int j = 0; j < s.length; j++) {

						String sname = s[j].getName();

						String sformat = sname.substring(3);

						if(sname.startsWith("get") && sformat.equals(dformat)) {

							Object value = null;

							value = s[j].invoke(source);

							Object[] args = new Object[1];

							args[0] = value;

							d[i].invoke(dest, args);

						}

					}

				}

			}

		} catch (IllegalAccessException e) {
			// TODO

		} catch (InvocationTargetException e) {

			// TODO

		}

	}

	/**
	 * 目标对象为null时才复制
	 * @param source
	 * @param dest
	 */
	public static void copyOnlyNull(Object source, Object dest) {

		Method s[] = source.getClass().getMethods();
		Method d[] = dest.getClass().getMethods();

		try {
			for (int i = 0; i < d.length; i++) {
				String dname = d[i].getName();
				String dformat = dname.substring(3);
				if (dname.startsWith("set")) {
					for (int m = 0; m < d.length; m++) {
						if (m == i) {
							continue;
						}
						String mname = d[m].getName();
						String mformat = mname.substring(3);
						if (mname.startsWith("get")
								&& mformat.equalsIgnoreCase(dformat)) {
							Object getVal = null;
							getVal = d[m].invoke(dest);
							if (getVal == null) {
								for (int j = 0; j < s.length; j++) {
									String sname = s[j].getName();
									String sformat = sname.substring(3);
									if (sname.startsWith("get")
											&& sformat.equals(dformat)) {
										Object value = null;
										value = s[j].invoke(source);
										Object[] args = new Object[1];
										args[0] = value;
										d[i].invoke(dest, args);
										break;
									}
								}
							}
							break;
						}
					}
				}
			}
		} catch (IllegalAccessException e) {
		} catch (InvocationTargetException e) {
		}
	}

	/**
	 * 
	 * @param oldObj
	 * @param newObj
	 * @param checkField
	 * @return
	 */
	public static Boolean objectCheckChange(Object oldObj, Object newObj,
			Vector checkField) {
		Method s[] = oldObj.getClass().getMethods();
		Method d[] = newObj.getClass().getMethods();
		Boolean ret = false;
		try {
			if (checkField == null) {
				for (int i = 0; i < s.length; i++) {
					String sName = s[i].getName();
					String sfName = s[i].getName().substring(3);
					if (sName.startsWith("get")) {
						Object sValue = s[i].invoke(oldObj);
						for (int j = 0; j < d.length; i++) {
							String dName = d[i].getName();
							String dfName = d[i].getName().substring(3);
							if (dName.startsWith("get")
									&& dfName.equalsIgnoreCase(sfName)) {
								Object dValue = d[i].invoke(newObj);
								if (!dValue.equals(sValue)) {
									return true;
								}
								break;
							}
						}
					}
				}
			} else {
				Iterator fIter = checkField.iterator();
				while (fIter.hasNext()) {
					String pName = (String) fIter.next();
					for (int i = 0; i < s.length; i++) {
						String sName = s[i].getName();
						String sfName = s[i].getName().substring(3);
						if (sName.startsWith("get")
								&& sfName.equalsIgnoreCase(pName)) {
							Object sValue = s[i].invoke(oldObj);
							for (int j = 0; j < d.length; i++) {
								String dName = d[i].getName();
								String dfName = d[i].getName().substring(3);
								if (dName.startsWith("get")
										&& dfName.equalsIgnoreCase(sfName)) {
									Object dValue = d[i].invoke(newObj);
									if (!dValue.equals(sValue)) {
										return true;
									}
									break;
								}
							}
							break;
						}
					}
				}
			}

		} catch (Exception ex) {

		}

		return ret;

	}

}
